package view.Core;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import fyp.mobile.CommentActivity;
import fyp.mobile.HistoryActivity;
import fyp.mobile.SearchCriteriaActivity;
import fyp.mobile.SearchDistrictActivity;
import fyp.mobile.SearchTypeActivity;
import method.camera.QRDecodeCamera;
import android.app.*;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.app.ProgressDialog;

public class encodeActivity extends Activity {

	private static final int REQUEST_CODE = 3434;
	EditText findtree_TV;
	ImageButton findtree_imageButton;
	Button findtree_button;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.findtree);

		findViewByID();

	}

	public void findTree(View view) {

		try {
			String url = getResources().getString(R.string.serverURL)
					+ "getJsonByQR.php?inputQR=" + findtree_TV.getText().toString();
			// getJsonByTreeID.php
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			String reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply = line;
			}
			reader.close();
			try {
				JSONArray json = new JSONArray(reply);

				JSONArray jsonArrayB = new JSONArray(json.getString(0));
				String getField = jsonArrayB.getString(0);
				// JSONObject jsonArray = new JSONObject(treeData);
				JSONObject jsonField = new JSONObject(getField);
				String treeIDValue = jsonField.getString("treeID");
				Intent i = new Intent();
				i.putExtra("treeID", treeIDValue);
				i.setClass(this, TabActivityX.class);
				startActivity(i);

			} catch (JSONException e) {
				toastMsg("This is not a QR code of treeID.");
			}
		} catch (Exception e) {
			toastMsg("" + e);
		}
		/*
		Intent intent = new Intent();
		intent.setClass(this, TabActivityX.class);
		startActivity(intent);
		finish();
		*/
		// Bundle bundle = new Bundle();
		// bundle.putString("QRid","Q0001");
		// intent.putExtras(bundle);

	}

	public void CallQrDecode(View view) {
		Intent intent = new Intent();
		Intent i = new Intent(this, encodeActivitySub.class);
		startActivityForResult(i, REQUEST_CODE);
		// Toast.makeText(this, "decodeString", Toast.LENGTH_LONG).show();

	}

	private void findViewByID() {
		// TODO Auto-generated method stub

		findtree_TV = (EditText) findViewById(R.id.findtree_TV);
		findtree_imageButton = (ImageButton) findViewById(R.id.findtree_imageButton);
		findtree_button = (Button) findViewById(R.id.findtree_button);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		super.onCreateOptionsMenu(menu);
		
		
		menu.add(Menu.NONE, Menu.FIRST + 1, Menu.NONE, "You want to search by :   ").setShortcut(
				'0', 'a');
		menu.add(Menu.NONE, Menu.FIRST + 2, Menu.NONE, "Cruteria").setShortcut(
				'1', 'b');
		menu.add(Menu.NONE, Menu.FIRST + 3, Menu.NONE, "District").setShortcut(
				'2', 'c');
		menu.add(Menu.NONE, Menu.FIRST + 4, Menu.NONE, "Type").setShortcut('3',
				'd');
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		String title = item.getTitle().toString();
		switch (item.getItemId()) {

		case Menu.FIRST + 2:
			Intent intentCruteria = new Intent();
			intentCruteria.setClass(this, SearchCriteriaActivity.class);
			startActivity(intentCruteria);
			break;
		case Menu.FIRST + 3:
			Intent intentDistrict = new Intent();
			intentDistrict.setClass(this, SearchDistrictActivity.class);
			startActivity(intentDistrict);
			break;
		case Menu.FIRST + 4:
			Intent intentType = new Intent();
			intentType.setClass(this, SearchTypeActivity.class);
			startActivity(intentType);
			break;
		}

		return super.onOptionsItemSelected(item);

	}

	private void show_alert() {

		AlertDialog.Builder alert_box = new AlertDialog.Builder(this);
		alert_box.setMessage("Do you want to quit the app?");
		alert_box.setPositiveButton("Yes",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						onDestroy();
					}
				});

		alert_box.setNegativeButton("No",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});

		alert_box.show();
	}

	public void toastMsg(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra("decodeString")) {
				String decodeString = data.getExtras()
						.getString("decodeString");
				// findtree_TV.setText(decodeString);
				findtree_TV.setText(decodeString);
				Toast.makeText(this, decodeString, Toast.LENGTH_LONG).show();
				// /String playTime =
				// data.getExtras().getString("decodeString");

			}
		}
	}

}